package com.ibeeking.gmc.global.g.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.gmc.global.api.common.param.TenantDetailParam;
import com.ibeeking.gmc.global.service.common.param.TenantParam;
import com.ibeeking.gmc.global.service.common.query.TenantEffectiveQuery;
import com.ibeeking.gmc.global.service.common.query.TenantPageQuery;
import com.ibeeking.gmc.global.service.common.query.TenantQuery;
import com.ibeeking.gmc.global.service.common.vo.TenantDetailVO;
import com.ibeeking.gmc.global.service.common.vo.TenantEffectiveVO;
import com.ibeeking.gmc.global.service.common.vo.TenantVO;
import com.ibeeking.gmc.global.service.common.vo.TenantViewVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName TenantController
 * @Description
 * @Author ibeeking
 * @Date 2020-12-24 09:21
 **/
@RestController
@RequestMapping("/g/tenant/v1")
public class TenantController {

    @Resource
    private com.ibeeking.gmc.global.g.rest.service.ITenantService tenantServiceImpl;

    @Resource
    private com.ibeeking.gmc.global.g.rest.service.ITenantDetailService tenantDetailServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户分页查询")
    @PostMapping(value = "/pageList")
    public Page<TenantVO> pageList(@RequestBody TenantPageQuery tenantPageQuery) {
        return tenantServiceImpl.pageList(tenantPageQuery);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增租户")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) TenantParam tenantParam) {
        tenantParam.setCreateBy(currentUser.getId()).setModifyBy(currentUser.getId());
        return tenantServiceImpl.add(tenantParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "租户修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) TenantParam tenantParam) {
        tenantParam.setModifyBy(currentUser.getId());
        return tenantServiceImpl.modify(tenantParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户详情查询")
    @PostMapping(value = "/view/{id}")
    public TenantVO view(@PathVariable("id") Integer id) {
        return tenantServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户认证查询")
    @PostMapping(value = "/viewCertInfo/{tenantid}")
    public TenantViewVO viewCertInfo(@PathVariable("tenantid") Integer tenantid) {
        return tenantServiceImpl.viewCertInfo(tenantid);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户详情介绍查询")
    @PostMapping(value = "/viewDetailInfo/{type}/{tenantid}")
    public TenantDetailVO viewDetailInfo(@PathVariable("type") Integer type, @PathVariable("tenantid") Integer tenantid) {
        return tenantDetailServiceImpl.viewByTenantId(type, tenantid);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增/修改租户详情")
    @PostMapping(value = "/addOrUpdateDetail")
    public Boolean addOrUpdateDetail(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) TenantDetailParam param) {
        return tenantDetailServiceImpl.save(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "租户发布状态")
    @PostMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        return tenantServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除租户")
    @PostMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Integer id) {
        return tenantServiceImpl.del(id);
    }


    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户列表查询")
    @PostMapping(value = "/list")
    public Page<TenantVO> list(@RequestBody TenantQuery tenantQuery) {
        return tenantServiceImpl.list(tenantQuery);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "初始化租户")
    @GetMapping(value = "/init/{tenantId}/{datasourceId}")
    public Boolean initTenant(@LoginUser CurrentUser currentUser, @PathVariable("tenantId") @NotNull(message = "tenantId不能为空") Integer tenantId, @PathVariable("datasourceId") @NotNull(message = "没有指定数据源") Long datasourceId) {
        return tenantServiceImpl.initTenant(currentUser.getId(), tenantId, datasourceId);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "读取租户初始化进度日志")
    @PostMapping(value = "/tenantInitLog/{tenantId}")
    public String readTenantInitLog(@LoginUser CurrentUser currentUser, @PathVariable("tenantId") @NotNull(message = "tenantId不能为空") Integer tenantId) {
        return tenantServiceImpl.readTenantInitLog(currentUser.getId(), tenantId);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "回收租户数据")
    @GetMapping(value = "/recovery/{tenantId}")
    public Boolean recovery(@LoginUser CurrentUser currentUser, @PathVariable("tenantId") @NotNull(message = "tenantId不能为空") Integer tenantId) {
        return tenantServiceImpl.recovery(currentUser.getId(), tenantId);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户列表查询")
    @PostMapping(value = "/effectiveList")
    public List<TenantEffectiveVO> effectiveList(@RequestBody TenantEffectiveQuery query) {
        return tenantServiceImpl.effectiveList(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询租户配置菜单Id")
    @GetMapping(value = "/queryPermissionIdsByTenantId/{tenantId}/{module}")
    public List<String> queryPermissionIdsByTenantId(@PathVariable("tenantId") @NotNull(message = "租户不能为空") Integer tenantId, @PathVariable("module") @NotBlank(message = "模块不能为空") String module) {
        return tenantServiceImpl.queryPermissionIdsByTenantId(tenantId, module);
    }

}
